<?php
session_start();
//Create by Nguyen 15/08/2012
require_once 'openid.php';
require_once('../../../../config.php');
require_once(DIR_SYSTEM . 'database/mysql.php');
require_once (DIR_SYSTEM .'helper/customer.php');
require_once (DIR_SYSTEM.'engine/registry.php');
require_once (DIR_SYSTEM.'library/db.php');
require_once (DIR_SYSTEM.'library/session.php');




try {
    # Change 'localhost' to your domain name.
    $openid = new LightOpenID('99xu.vn');
    if(!$openid->mode) {
        if(isset($_GET['openid_identifier'])) {
            $openid->identity = $_GET['openid_identifier'];
			$openid->required = array('namePerson/friendly', 'contact/email','namePerson','namePerson/first','namePerson/last','phone/fax','phone/cell','contact/IM/Skype','contact/IM/Yahoo');
            header('Location: ' . $openid->authUrl());
        }
    } elseif($openid->mode == 'cancel') {
        //echo 'User has canceled authentication!';
		header('Location:' .HTTP_SERVER. 'index.php');
    } else {
		$returnVariables = $openid->getAttributes();
		$email = $returnVariables['contact/email'];
		$_SESSION['mainemail'] = $email;
	//-----------Edit by Nguyen Hai Dang 5/10/2012---	
		if ($returnVariables)
			{
				
				$session = new Session();
					Registry::set('session', $session);
				$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
					Registry::set('db', $db);
				$sql = $db->query("SELECT * FROM customer where LOWER(email) = '$email'");
				if(!$sql->num_rows){
					$db->query("INSERT INTO customer (email,status,ym,username) values('$email','1','nick_0_trung#@^!','$email')");
				}
				$login = new HelperCustomer();
				$login->login($email,'',true);
				
			}		
		else 
			header('Location:' .HTTP_SERVER. 'index.php?route=account/create');	
			
		if(strpos($email,"gmail"))	header('Location: ' . HTTP_SERVER . 'catalog/controller/account/plugin/Google/ReadyGetContact.php');
		else header('Location: ' . HTTP_SERVER . 'catalog/controller/account/plugin/Yahoo/getreqtok.php');
    }
	//---------End edit-------------
} catch(ErrorException $e) {
    echo $e->getMessage();
}
?>
